<?php

class PdoReunion extends PdoConnect {

    public function create(reunion $reunion) {
        try {
            $this->open();
            $sql = "INSERT INTO `reunion`(IDREUNION, NOMREUNION, DATEREUNION, IDLIEU) VALUES(?, ?, ?, ?)";

            $prep = $this->pdo->prepare($sql);


            $prep->bindValue(1, $reunion->getIdReunion(), PDO::PARAM_INT);
            $prep->bindValue(2, $reunion->getNomReunion(), PDO::PARAM_STR);
            $prep->bindValue(3, $reunion->getDateReunion(), PDO::PARAM_STR);
            $prep->bindValue(4, $reunion->getIdLieu(), PDO::PARAM_INT);

            $prep->execute();
        } catch (PDOException $e) {
            $msg = 'ERREUR PDO dans ' . $e->getFile() . ' L.' . $e->getLine() . ' : ' . $e->getMessage();
            die($msg);
        }
        $this->close();
    }

    public function readAll() {
        try {
            $this->open();
            $sql = "SELECT * FROM `reunion` ORDER BY DATEREUNION ";
            $prep = $this->pdo->prepare($sql);

            $prep->execute();
            $prep->setFetchMode(PDO::FETCH_ASSOC);


            return $prep;
        } catch (PDOException $e) {
            $msg = 'ERREUR PDO dans ' . $e->getFile() . ' L.' . $e->getLine() . ' : ' . $e->getMessage();
            die($msg);
        }
        $this->close();
    }

    
    public function delete(reunion $reunion) {

        try {
            $this->open();
            $sql = "DELETE FROM reunion WHERE IDREUNION=?";

            $prep = $this->pdo->prepare($sql);

            $prep->bindValue(1, $reunion->getIdReunion(), PDO::PARAM_INT);
            $prep->execute();
        } catch (PDOException $e) {
            $msg = 'ERREUR PDO dans ' . $e->getFile() . ' L.' . $e->getLine() . ' : ' . $e->getMessage();
            die($msg);
        }
        $this->close();
    }

    public function update(reunion $reunion) {
        try {

            $this->open();
            $sql = "UPDATE reunion SET NOMREUNION= ?, DATEREUNION= ?, IDLIEU=? WHERE IDREUNION=?";

            $prep = $this->pdo->prepare($sql);

            $prep->bindValue(1, $reunion->getNomReunion(), PDO::PARAM_STR);
            $prep->bindValue(2, $reunion->getDateReunion(), PDO::PARAM_STR);
            $prep->bindValue(3, $reunion->getIdLieu(), PDO::PARAM_INT);
            $prep->bindValue(4, $reunion->getIdReunion(), PDO::PARAM_INT);

            $prep->execute();
        } catch (PDOException $e) {
            $msg = 'ERREUR PDO dans ' . $e->getFile() . ' L.' . $e->getLine() . ' : ' . $e->getMessage();
            die($msg);
        }
        $this->close();
    }

    public function exist($IdReunion) {
        try {

            $this->open();
            $query = "SELECT * FROM reunion WHERE ='$IdReunion';";
            $statement = $this->pdo->query($query);
            return $statement->rowCount();
        } catch (PDOException $e) {
            $msg = 'ERREUR PDO dans ' . $e->getFile() . ' L.' . $e->getLine() . ' : ' . $e->getMessage();
            die($msg);
        }
        $this->close();
    }

    public function readOne($IdReunion) {
        try {
            $this->open();
            $sql = "SELECT * From `reunion` Where IdReunion=$IdReunion";
            $prep = $this->pdo->prepare($sql);

            $prep->execute();

            $reunion = new Reunion();
            $ligne = $prep->fetch(PDO::FETCH_OBJ);
            $reunion->setIdReunion($ligne->IDREUNION);
            $reunion->setNomReunion($ligne->NOMREUNION);
            $reunion->setDateReunion($ligne->DATEREUNION);
            $reunion->setIdLieu($ligne->IDLIEU);
            return $reunion;
        } catch (PDOException $e) {
            $msg = 'ERREUR PDO dans ' . $e->getFile() . ' L.' . $e->getLine() . ' : ' . $e->getMessage();
            die($msg);
        }
    }

}
